package com.yoda.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yoda.constant.MessageConstant;
import com.yoda.dto.AdvImgDto;
import com.yoda.dto.AdvImgPageQuery;
import com.yoda.entity.YddAdvimg;
import com.yoda.entity.YddAdvpos;
import com.yoda.mapper.YddAdvimgMapper;
import com.yoda.mapper.YddAdvposMapper;
import com.yoda.result.PageResult;
import com.yoda.result.Result;
import com.yoda.service.IYddAdvimgService;
import com.yoda.vo.AdvImgVo;
import com.yoda.vo.YddAdvImgVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-05-15
 */
@Service
@Slf4j
public class YddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper, YddAdvimg> implements IYddAdvimgService {
    @Autowired
    private YddAdvimgMapper yddAdvimgMapper;
    @Autowired
    private YddAdvposMapper advposMapper;

    /**
     * 广告图详情
     * @param id
     * @return
     */
    @Override
    public Result selectById(String id) {
        //调用方法进行单一查询
        Integer advId = Integer.valueOf(id);
        YddAdvimg advimg = this.getById(advId);
        return Result.success(advimg);
    }

    /**
     * 广告图列表-分页
     * @param adv
     * @return
     */
    @Override
    public Result pageQuery(AdvImgPageQuery adv) {
        //进行非空判断
        if (adv.getPage() == null){
            adv.setPage(1+"");
        }
        if (adv.getLimit() == null){
            adv.setLimit(10+"");
        }



        //获取第几页和每页显示多少条
        Integer page = Integer.valueOf(adv.getPage());
        Integer size = Integer.valueOf(adv.getLimit());
        //调用方法进行分页
        IPage<YddAdvimg> iPage = new Page(page,size);
        log.info("{}" ,iPage);
        LambdaQueryWrapper<YddAdvimg> wrapper = Wrappers.lambdaQuery();
        wrapper.like(YddAdvimg::getAdvimgdesc,adv.getAdvimgdesc()).like(YddAdvimg::getAdvimgpos,adv.getAdvimgpos());
        yddAdvimgMapper.selectPage(iPage, wrapper);

        AdvImgVo vo = new AdvImgVo();
        List<YddAdvImgVo> imgVo = new ArrayList<>();

        List<YddAdvimg> records = iPage.getRecords();
        for (YddAdvimg record : records) {
            YddAdvImgVo imgVo1 = new YddAdvImgVo();
            String advimgpos = record.getAdvimgpos();
            YddAdvpos yddAdvpos = advposMapper.selectById(advimgpos);
            imgVo1.setAdvposname(yddAdvpos.getAdvposname());
            BeanUtils.copyProperties(record,imgVo1);
            imgVo.add(imgVo1);
        }

        vo.setCount((int) iPage.getTotal());
        vo.setCurrentPage(page);
        vo.setData(imgVo);
        vo.setPageSize(size);
        vo.setTotalPages((int) iPage.getTotal());
        return Result.success(vo);
    }

    /**
     * 广告图修改
     * @param dto
     * @return
     */
    @Override
    public Result updateAdvImage(AdvImgDto dto) {
        //判断id是否为空
        if (dto.getId() == null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        YddAdvimg advimg = this.getById(dto.getId());
        BeanUtils.copyProperties(dto,advimg);
        //id条件开启
        LambdaQueryWrapper<YddAdvimg> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(YddAdvimg::getId,dto.getId());
        //开始修改并加上条件
        this.saveOrUpdate(advimg,wrapper);
        //返回成功
        return Result.success();
    }

    /**
     * 根据id进行广告图删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(String id) {
        //强转成int类型然后进行删除并返回结果
        Integer imgId = Integer.valueOf(id);
        boolean b = this.removeById(imgId);
        return Result.success(b);
    }

    /**
     * 广告图添加
     * @param advimg
     * @return
     */
    @Override
    public Result addAdvImg(YddAdvimg advimg) {
        this.save(advimg);
        return Result.success();
    }
}
